const serviceUrl = 'https://service.aside.fun/api/v1' //'http://192.168.1.109:3000/api/v1'
//'https://service.aside.fun/api/v1'

const checkLogin = function(res) {
  if (res && res.data && res.data.code == 401) {
    wx.showModal({
      title: '温馨提示',
      content: res.data.msg || '请求失败',
      success() {
        wx.reLaunch({
          url: '/pages/me/me'
        })
      }
    })
  } else {
    wx.showModal({
      title: '温馨提示',
      content: res.data.msg || '请求失败',
    })
  }

}

const _post = function(url, data) {
  return new Promise((resolve) => {
    try {
      let token = wx.getStorageSync('auth') ? wx.getStorageSync('auth').token || '' : '',
        header = {}
      if (token) {
        header['authorization'] = `Bearer ${token}`
      }
      wx.request({
        url: serviceUrl + url,
        method: 'POST',
        data,
        header,
        success(res) {
          if (res.data.code === 0) {
            resolve(res.data)
          } else {
            checkLogin(res)
          }
          resolve(res.data)
        },
        fail(err) {
          console.log(err)
          wx.showModal({
            title: '温馨提示',
            content: err.errMsg === 'request:fail ' ? '网络开小差啦？' : JSON.stringify(err),
            showCancel: false
          })
          resolve({
            code: 1000,
            data: null,
            msg: '出现错误'
          })
        }
      })
    } catch (err) {
      console.log(err)
      wx.showModal({
        title: '温馨提示',
        content: err.errMsg === 'request:fail ' ? '网络开小差啦？' : JSON.stringify(err),
        showCancel: false
      })
      resolve({
        code: 1000,
        data: null,
        msg: '出现错误'
      })
    }
  })
}

const _get = function(url, data) {
  return new Promise((resolve) => {
    try {
      let token = wx.getStorageSync('auth') ? wx.getStorageSync('auth').token || '' : '',
        header = {}
      if (token) {
        header['authorization'] = `Bearer ${token}`
      }
      wx.request({
        url: serviceUrl + url,
        method: 'GET',
        data,
        header,
        success(res) {
          if (res.data.code === 0) {
            resolve(res.data)
          } else {
            checkLogin(res)
          }
          resolve(res.data)
        },
        fail(err) {
          console.log(err)
          wx.showModal({
            title: '温馨提示',
            content: err.errMsg === 'request:fail ' ? '网络开小差啦？' : JSON.stringify(err),
            showCancel: false
          })
          resolve({
            code: 1000,
            data: null,
            msg: '出现错误'
          })
        }
      })
    } catch (err) {

      wx.showModal({
        title: '温馨提示',
        content: err.errMsg === 'request:fail ' ? '网络开小差啦？ ' : JSON.stringify(err),
        showCancel: false
      })
      resolve({
        code: 1000,
        data: null,
        msg: '出现错误'
      })
    }
  })
}

const _request = function(url, method, data) {
  return new Promise((resolve) => {
    try {
      let token = wx.getStorageSync('auth') ? wx.getStorageSync('auth').token || '' : '',
        header = {}
      if (token) {
        header['authorization'] = `Bearer ${token}`
      }
      wx.request({
        url: serviceUrl + url,
        method,
        data,
        header,
        success(res) {
          if (res.data.code === 0) {
            resolve(res.data)
          } else {
            checkLogin(res)

          }
          resolve(res.data)
        },
        fail(err) {
          console.log(err)
          wx.showModal({
            title: '温馨提示',
            content: err.errMsg === 'request:fail ' ? '网络开小差啦？' : JSON.stringify(err),
            showCancel: false
          })
          resolve({
            code: 1000,
            data: null,
            msg: '出现错误'
          })
        }
      })
    } catch (err) {

      wx.showModal({
        title: '温馨提示',
        content: err.errMsg === 'request:fail ' ? '网络开小差啦？ ' : JSON.stringify(err),
        showCancel: false
      })
      resolve({
        code: 1000,
        data: null,
        msg: '出现错误'
      })
    }
  })
}

module.exports = {
  _post,
  _get,
  _request
}